package ui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.TibNeeg;
import model.YawmSaub;
import control.Control_UI;

/**
 * Dialog to creat a new Txiv and links to Cov Menyuam
 * @author Kao Vang
 *
 */
public class DialogNtxivTxiv extends JDialog{
	
	/**
	 * Default serial UID
	 */
	private static final long serialVersionUID = 1L;
	private Control_UI cltr_ui;
	private TibNeeg kuv_tn;
	private YawmSaub yawmSaub;
	private ButtonNtxivTxivListener listener;
	
	/* Components in this dialog*/
	private JPanel panel_main;
	private JPanel panel_fiel_npe;
	//private JPanel panel_fiel_xeem;
	
	private JPanel panel_button;
	private JTextField fiel_npe;
	private JTextField fiel_xeem; 
	private JButton button_add;
	private JButton button_cancel;
	
	/**
	 * Contructor with a frame that the dialog will dispaly on
	 * @param frame - the frame dialog will display on
	 */
	public DialogNtxivTxiv( TibNeeg kuv_tn, YawmSaub yawmSaub) {

		this.cltr_ui = Control_UI.getInstance();
		this.kuv_tn = kuv_tn;
		this.yawmSaub = yawmSaub;
		
		this.setTitle("Ntxiv txiv rau huav family tree");
		this.setSize(new Dimension(500, 250));
		//this.setLocationRelativeTo(cltr_ui.getFrame().getWindow());
		Point point = cltr_ui.getFrame().getWindow().getLocation(); 
		int x = (int)point.getX();
		int y = (int)point.getY();
		Dimension dim = cltr_ui.getFrame().getWindow().getSize();
		this.setLocation(new Point(x+((int)dim.getWidth()/2)- this.getWidth()/2, y+((int)dim.getHeight()/2)- this.getHeight()/2 ));
		this.setAlwaysOnTop(true);
		
		/* Create all components */
		panel_main = new JPanel();
		panel_main.setBorder(BorderFactory.createTitledBorder("Ntxiv neeg"));		// Border 
		panel_main.setLayout(new BoxLayout(panel_main, BoxLayout.PAGE_AXIS));	// 
		
		panel_fiel_npe = new JPanel();
		//panel_fiel_xeem = new JPanel();
		panel_button = new JPanel();
		
		panel_fiel_npe.add(new JLabel("Npe :"));
		fiel_npe = new JTextField(15);
		panel_fiel_npe.add(fiel_npe);
		
		panel_fiel_npe.add(new JLabel("Xeem :"));
		//panel_fiel_xeem.add(new JLabel("Xeem :"));
		fiel_xeem = new JTextField(15);
		//panel_fiel_xeem.add(fiel_xeem);
		fiel_xeem.setText(kuv_tn.getXeem());
		panel_fiel_npe.add(fiel_xeem);
		
		listener = new ButtonNtxivTxivListener();
		button_add = new JButton("Ntxiv");
		button_add.addActionListener(listener);
		panel_button.add(button_add);
		
		button_cancel = new JButton("Cancel");
		button_cancel.addActionListener(listener);
		panel_button.add(button_cancel);
		
		panel_main.add(panel_fiel_npe);
		//panel_main.add(panel_fiel_xeem);
		panel_main.add(panel_button);
		
		this.getContentPane().add(panel_main);
		
		showDial();
	}
	
	/**
	 * Show dialog popup
	 */
	public void showDial() {
		this.setVisible(true);
	}
	
	/**
	 * Hide dialog popup
	 */
	public void hideDial() {
		this.setVisible(false);
	}
	
	
	/* ++++++++ Class Listener ++++++++ */
	private class ButtonNtxivTxivListener implements ActionListener {
		  public void actionPerformed (ActionEvent event) {
		    Object source = event.getSource();
		    
		
		    if (source == button_add) {
			   	/* Get informations from form */
			   	String npe = fiel_npe.getText();
			   	String xeem = fiel_xeem.getText();
			   	npe = npe.trim();
			   	xeem = xeem.trim();
			    	
			   	/* Launch the modification */
				yawmSaub.createTxivTshiab(npe, xeem, kuv_tn, null);
				yawmSaub.refreshTree();
				
			    hideDial();
			}

			if (source == button_cancel) {
		    	// Try to close, not hidden
		        hideDial();
		        
		    }
		  }
	 }
	
	
	
}
